package com.ruoyi.womp.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.womp.domain.WompOsiRecordItem;
import com.ruoyi.womp.mapper.WompOsiRecordMapper;
import com.ruoyi.womp.domain.WompOsiRecord;
import com.ruoyi.womp.service.IWompOsiRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 巡检记录Service业务层处理
 * 
 * @author mzz
 * @date 2023-05-11
 */
@Service
public class WompOsiRecordServiceImpl implements IWompOsiRecordService 
{
    @Autowired
    private WompOsiRecordMapper wompOsiRecordMapper;

    /**
     * 查询巡检记录
     * 
     * @param id 巡检记录主键
     * @return 巡检记录
     */
    @Override
    public WompOsiRecord selectWompOsiRecordById(Integer id)
    {
        return wompOsiRecordMapper.selectWompOsiRecordById(id);
    }

    /**
     * 查询巡检记录列表
     * 
     * @param wompOsiRecord 巡检记录
     * @return 巡检记录
     */
    @Override
    public List<WompOsiRecord> selectWompOsiRecordList(WompOsiRecord wompOsiRecord)
    {
        return wompOsiRecordMapper.selectWompOsiRecordList(wompOsiRecord);
    }

    /**
     * 新增巡检记录
     * 
     * @param wompOsiRecord 巡检记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWompOsiRecord(WompOsiRecord wompOsiRecord)
    {
        wompOsiRecord.setCreateTime(DateUtils.getNowDate());
        int rows = wompOsiRecordMapper.insertWompOsiRecord(wompOsiRecord);
        insertWompOsiRecordItem(wompOsiRecord);
        return rows;
    }

    /**
     * 修改巡检记录
     * 
     * @param wompOsiRecord 巡检记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWompOsiRecord(WompOsiRecord wompOsiRecord)
    {
        wompOsiRecord.setUpdateTime(DateUtils.getNowDate());
        wompOsiRecordMapper.deleteWompOsiRecordItemByOsiRecordId(wompOsiRecord.getId());
        insertWompOsiRecordItem(wompOsiRecord);
        return wompOsiRecordMapper.updateWompOsiRecord(wompOsiRecord);
    }

    /**
     * 批量删除巡检记录
     * 
     * @param ids 需要删除的巡检记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWompOsiRecordByIds(String ids)
    {
        wompOsiRecordMapper.deleteWompOsiRecordItemByOsiRecordIds(Convert.toStrArray(ids));
        return wompOsiRecordMapper.deleteWompOsiRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除巡检记录信息
     * 
     * @param id 巡检记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWompOsiRecordById(Integer id)
    {
        wompOsiRecordMapper.deleteWompOsiRecordItemByOsiRecordId(id);
        return wompOsiRecordMapper.deleteWompOsiRecordById(id);
    }

    /**
     * 新增巡检记录明细信息
     * 
     * @param wompOsiRecord 巡检记录对象
     */
    public void insertWompOsiRecordItem(WompOsiRecord wompOsiRecord)
    {
        List<WompOsiRecordItem> wompOsiRecordItemList = wompOsiRecord.getWompOsiRecordItemList();
        Integer id = wompOsiRecord.getId();
        if (StringUtils.isNotNull(wompOsiRecordItemList))
        {
            List<WompOsiRecordItem> list = new ArrayList<WompOsiRecordItem>();
            for (WompOsiRecordItem wompOsiRecordItem : wompOsiRecordItemList)
            {
                wompOsiRecordItem.setOsiRecordId(id);
                list.add(wompOsiRecordItem);
            }
            if (list.size() > 0)
            {
                wompOsiRecordMapper.batchWompOsiRecordItem(list);
            }
        }
    }
}
